// 另一个玩家订阅开始游戏
NAF.connection.subscribeToDataChannel(dataType_startShootGame, function (senderId, dataType, data, targetId) {
    // 另一玩家启动游戏
    console.error('Remotelly start shoot game');
    startShootGame();
});

// 订阅左边用户信息
NAF.connection.subscribeToDataChannel(dataType_userInfo_left, function (senderId, dataType, data, targetId) {
    const info_left = document.querySelector('#id_left');
    const info_right = document.querySelector('#id_right');
    const info_leftValue = info_left.getAttribute('text').value;
    const info_rightValue = info_right.getAttribute('text').value;

    if (info_leftValue === 'id_left') {
        info_left.setAttribute('text', {
            value: data.localuserId
        })
    }
    // console.error('LEFT');
    // console.log('senderId', senderId);
    // console.log('dataType', dataType);
    // console.log('data', data);
    // console.log('targetId', targetId);
    // console.log('localuserId', localuserId);
});

// 订阅右边用户信息
NAF.connection.subscribeToDataChannel(dataType_userInfo_right, function (senderId, dataType, data, targetId) {
    const info_left = document.querySelector('#id_left');
    const info_right = document.querySelector('#id_right');
    const info_leftValue = info_left.getAttribute('text').value;
    const info_rightValue = info_right.getAttribute('text').value;

    if (info_rightValue === 'id_right') {
        info_right.setAttribute('text', {
            value: data.localuserId
        })
    }
});

// 订阅改变左边用户分数
NAF.connection.subscribeToDataChannel(dataType_userScore_left, function (senderId, dataType, data, targetId) {
    // console.error('remotely dataType_userScore_left');
    const score_left = document.querySelector('#score_left');
    setScore(score_left, data.score);
});

// 订阅改变右边用户分数
NAF.connection.subscribeToDataChannel(dataType_userScore_right, function (senderId, dataType, data, targetId) {
    // console.error('remotely dataType_userScore_right');
    const score_right = document.querySelector('#score_right');
    setScore(score_right, data.score);
});

// 除了两个玩家外的用户订阅游戏结果
NAF.connection.subscribeToDataChannel(dataType_shootGame_result_others, function (senderId, dataType, data, targetId) {
    const {player1Id,player2Id,winnerId,failurerId,isEqual}=data;
    // 被广播的用户不能是参赛用户，不然会重复提示
    if(player1Id!==localuserId &&player2Id!==localuserId){
        if(isEqual===true){
            showMessage_shootGame(`${player1Id} 和 ${player2Id}平局`,10000);
            // alert(`${player1Id} 和 ${player2Id}平局`);
        }else{
            showMessage_shootGame(`${winnerId}赢了,${failurerId}输了`,10000);
            // alert(`${winnerId}赢了,${failurerId}输了`)
        }
    }
    // 所有玩家都清空记分板上信息
    console.error('remotelly resetScoreBoard');
    resetScoreBoard();
});

// 第三者(游戏开始后新进入房间)订阅初始值。包括记分板信息，battle-button锁
NAF.connection.subscribeToDataChannel(dataType_scoreBoardInitialize, function (senderId, dataType, data, targetId) {
    // 同步记分板信息
    const info_left = document.querySelector('#id_left');
    const info_right = document.querySelector('#id_right');
    const score_left = document.querySelector('#score_left');
    const score_right = document.querySelector('#score_right');
    setUserInfo(info_left,data.info_leftValue);
    setUserInfo(info_right,data.info_rightValue);
    setScore(score_left,data.originalScore_left);
    setScore(score_right,data.originalScore_right);

    // 同步battle-button状态
    shootGameLock=data.shootGameLock;
    
})